import serial, serial.tools.list_ports
class TIotUtils:
    def __init__(self):
        pass
    @staticmethod
    def getSerialClient( comPort , comBaudrate , comParity, timeout=4):
        res = None
        try:
            res = serial.Serial(comPort, comBaudrate, timeout=4 , parity=comParity)
        except Exception as er :
            print(er)
        return res
    @staticmethod
    def crc16( datas):
        crc =0
        crcH=0
        crcL=0
        try:
            crc = 0xFFFF
            # 循环处理每个数据字节
            for byte in datas:
                # 将每个数据字节与crc进行异或操作
                crc ^= byte

                # 对crc的每一位进行处理
                for _ in range(8):
                    # 如果最低位为1，则右移一位并执行异或0xA001操作(即0x8005按位颠倒后的结果)
                    if crc & 0x0001:
                        crc = (crc >> 1) ^ 0xA001
                    # 如果最低位为0，则仅将crc右移一位
                    else:
                        crc = crc >> 1
                crcL = crc % 256
                crcH = crc >> 8
        except Exception as er:
            print('crc error',er)
        return crc , crcH , crcL